import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import WindiCSS from "vite-plugin-windicss";
import { viteSingleFile } from "vite-plugin-singlefile";
import { visualizer } from "rollup-plugin-visualizer";

export default defineConfig({
  base: "./",
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"),
    },
  },
  server: {
    port: 8080,
    host: true,
    open: true,
    proxy: {
      "/api": {
        target: "https://szjy.cqedu.cn:8081",
        changeOrigin: true,
        rewrite: (p) => p.replace(/^\/api/, ""),
      },
    },
  },

  build: {
    target: "es2017", // 如果浏览器支持，可以升到 es2017 或 esnext，减少 polyfill
    cssCodeSplit: false, // 合并 CSS
    chunkSizeWarningLimit: 2000, // 调整提示阈值
    assetsInlineLimit: 4096, // 小于 4KB 的资源内联
    minify: "terser",
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true,
        pure_funcs: ["console.log"], // 强制删除 console.log
      },
      format: {
        comments: false,
      },
    },
    rollupOptions: {
      output: {
        manualChunks: undefined, // 保持单文件
      },
    },
  },

  plugins: [
    vue(),
    WindiCSS(),
    viteSingleFile(),
    visualizer({ filename: "./dist/stats.html" }), // 可视化分析体积
  ],
});
